package com.stn.zjx.givingtreasure.service;

import android.content.Context;
import android.widget.Toast;

import com.orhanobut.logger.Logger;
import com.stn.zjx.givingtreasure.database.GreenDaoManager;
import com.stn.zjx.givingtreasure.greendao.UserDao;
import com.stn.zjx.givingtreasure.table.User;


import java.util.Date;
import java.util.List;

import cn.jpush.android.api.JPushInterface;

/**
 * 用户账号操作
 */
public class UserService {
    public Context context;
    private UserService(){

    }
    public static final class UserServiceHolder{
        private final static UserService instance=new UserService();
    }
    //单例实例化
    public static UserService getInstance(Context context){
        return UserServiceHolder.instance;
    }


    //用户登录
    /**
     *
     * @param loginUser  需要登录的用户
     */
  public boolean login(User loginUser){
      try {
          User db_user= GreenDaoManager.getInstance()
                  .getDaoSession()
                  .getUserDao().queryBuilder()
                  .where(UserDao.Properties.UserId.eq(loginUser.getUserId()))
                  .unique();


          loginUser.setLogin_checked(1);
          if(db_user==null){
              GreenDaoManager.getInstance()
                      .getDaoSession()
                      .getUserDao()
                      .insert(loginUser);
          }else{
              loginUser.setId(db_user.getId());
              GreenDaoManager.getInstance()
                      .getDaoSession()
                      .getUserDao()
                      .update(loginUser);
          }
          List<User> queryall=GreenDaoManager.getInstance().getDaoSession()
                  .getUserDao()
                  .loadAll();
          if(queryall!=null&&queryall.size()!=0){
              for(User user:queryall){
                  if(user.getUserId()!=loginUser.getUserId()){
                      user.setLogin_checked(0);
                      GreenDaoManager.getInstance().getDaoSession()
                              .getUserDao()
                              .update(user);
                  }
              }
          }

      }catch (Exception e){
          e.printStackTrace();
          return false;
      }

      return true;
  }

  //获取在线用户对象
  public User getOnlineLoginUser(){
        try {
            User login_user = GreenDaoManager.getInstance()
                    .getDaoSession()
                    .getUserDao()
                    .queryBuilder()
                    .where(UserDao.Properties.Login_checked.eq(1)).orderDesc(UserDao.Properties.UserLoginDate)
                    .unique();
            if(login_user==null){
                return null;
            }

            return login_user;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
  }
  //在线用户退出登录
    public boolean exitLogin(){
        User login_user = GreenDaoManager.getInstance()
                .getDaoSession()
                .getUserDao()
                .queryBuilder()
                .where(UserDao.Properties.Login_checked.eq(1)).orderDesc(UserDao.Properties.UserLoginDate)
                .unique();
        if(login_user!=null){
            login_user.setLogin_checked(0);
            GreenDaoManager.getInstance()
                    .getDaoSession()
                    .getUserDao()
                    .update(login_user);
            return true;
        }else{
            return false;
        }
    }
   //用户更新个人信息，资料
    public User updateUser(User updateUser){
        User db_user= GreenDaoManager.getInstance()
                .getDaoSession()
                .getUserDao().queryBuilder()
                .where(UserDao.Properties.UserId.eq(updateUser.getUserId()))
                .unique();
        if(db_user!=null){
            updateUser.setId(db_user.getId());
            updateUser.setLogin_checked(db_user.getLogin_checked());
            GreenDaoManager.getInstance()
                    .getDaoSession()
                    .getUserDao()
                    .update(updateUser);
           return updateUser;
        }else{
           return null;
        }
    }
}
